﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ExportData;

namespace APSIM
{
    public partial class frmMaim : Form
    {
        public frmMaim()
        {
            InitializeComponent();
        }

        //从Excel读取数据到DataSet
        ExcelToDataset xlsto = new ExcelToDataset();
        //从DataSet保存数据到Excel
        DataSettoExcel dsto = new DataSettoExcel();
        //把保存的xls文件进行格式修改
        ExcelOperate xlsOperate = new ExcelOperate();


        string xlsFile = "";

        double TAV =0;
        double AMP = 0;
        double Latitude = 0;

        //Select Excel File
        private void btnSelect_Click(object sender, EventArgs e)
        {
            try
            {
                if (ofdExcelPathSelect.ShowDialog() == DialogResult.OK)
                {
                    //读取并显示
                    xlsFile = ofdExcelPathSelect.FileName;
                    txtExcelPath.Text = xlsFile;
                    DataSet ds = xlsto.ExcelToDataGridView(xlsFile);
                    ExcelDataView.DataSource = ds.Tables[0];
                    
                    //同时计算TAV和AMP
                    TAV = Math.Round( xlsto.GetAnnualTemAverage(xlsFile),2);
                    AMP = Math.Round( xlsto.GetAnnualTemAmplitude(xlsFile),2);
                    txtTav.Text = Convert.ToString( TAV);
                    txtAMP.Text = Convert.ToString( AMP);
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        //Export xls file to txt
        private void btnExportExcel_Click(object sender, EventArgs e)
        {
            try
            {
                lblMessage.Text = "";

                if (txtLatitude.Text == "")
                {
                    MessageBox.Show("请输入该地点的纬度（如-33.78）！","提示");
                }
                else
                {
                    Latitude = Convert.ToDouble(txtLatitude.Text);
                    //计算后结果保存到Excel
                    dsto.ExportToExcel(xlsto.MetToExcel(xlsFile), Path.Combine(Path.GetDirectoryName(xlsFile), "met.xls"));

                  xlsOperate.xlsFileOperate(Path.Combine(Path.GetDirectoryName(xlsFile), "met.xls"), Latitude.ToString(), TAV.ToString(), AMP.ToString(), Path.Combine(Path.GetDirectoryName(xlsFile), "met.met"));

                  lblMessage.Text = "成功保存" + Path.Combine(Path.GetDirectoryName(xlsFile), "met.met") + "";
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
       
        //状态栏
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.agri520.cn");
        }
    }
}
